package com.duoduo.selenium.demo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.duoduo.selenium.util.SeleniumUtil;

public class NeteaseYellowTest {

	public static void main(String[] args) {
		SeleniumUtil.setProperties();

		WebDriver driver = new FirefoxDriver();
		driver.get("http://mail.163.com/html/160513_yellow/");

		List<WebElement> elements = driver.findElements(By.xpath("//ul[@id='areaList']/li/a"));
		if (elements != null && !elements.isEmpty()) {
			for (WebElement element : elements) {
				System.out.println(element.getText());
				element.click();

				SeleniumUtil.waitme(500);

				List<WebElement> dataElements = driver.findElements(By.xpath("//div[@class='detailList']/ul/li"));
				if (dataElements != null && !dataElements.isEmpty()) {
					for (WebElement de : dataElements) {
						printData(de);
					}
				}
				System.out.println();

				SeleniumUtil.waitme(500);
			}
		}

		driver.close();
	}

	private static void printData(WebElement element) {
		System.out.println("\t" + formatText(getTextByClass(element, "name"), 30) + getTextByClass(element, "mail"));
	}

	private static String getTextByClass(WebElement parent, String className) {
		WebElement element = parent.findElement(By.className(className));
		return (element == null) ? "" : element.getText();
	}

	private static String formatText(String text, int len) {
		String blank = StringUtils.repeat("　", len);
		return (text + blank).substring(0, len);
	}
}
